package repositories;

import java.io.Serializable;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import util.Entity;

/**
 */
public class GenericDao<T> extends HibernateGenericDAO<T> {

    private static final long serialVersionUID = 1L;

    private Class<T> clazz;

    @Override
    protected Class<T> getDomainClass() {
        return this.clazz;
    }

    @Transactional
    public Serializable saveEntity(final T object) {
        Serializable id = this.getHibernateTemplate().save(object);
        this.getHibernateTemplate().flush();
        return id;
    }

    @SuppressWarnings("rawtypes")
    @Transactional
    public List findAllOf(final String className) {
        return this.getHibernateTemplate().find("from " + className + " o");
    }

    @SuppressWarnings("unchecked")
    public T findByIdAndClass(final Serializable id, final Class<? extends Entity> clax) {
        return (T) this.getHibernateTemplate().get(clax, id);
    }

    public Class<T> getClazz() {
        return this.clazz;
    }

    public void setClazz(final Class<T> clazz) {
        this.clazz = clazz;
    }
}
